
/**
 * @author murugan.kannan
 *
 */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JPanel;


public class Graph extends JPanel implements ActionListener{
	private Timer refreshTimer;
	static int scale =800,s_WIDTH=scale,s_HEIGHT=scale,size=scale/20,size_2=2*size;
	boolean needSmallLines=true,refresh=false;
//	static int arr[][]=new int[s_WIDTH/size][s_HEIGHT/size];
	public Graph() {
    	refreshTimer=new Timer(5, this);
    	refreshTimer.start();


	}
	@Override
	public void paint(Graphics g) {
		if(refresh){
		g.setColor(Color.white);
		g.fillRect(0, 0, s_WIDTH, s_HEIGHT);
		}
		for(int i=0;i<s_HEIGHT/size;i++){
			if(needSmallLines){
			g.drawLine(i*size, 0, i*size, s_WIDTH);
			g.drawLine(0,i*size,  s_HEIGHT,i*size );
			}
			if(i==s_HEIGHT/size_2){
				g.fillRect(i*size-2, 0, 4, s_WIDTH);
				g.fillRect(0,i*size-2,  s_HEIGHT,4 );
				
			}else
			if(i<s_HEIGHT/size_2){
				g.drawString((s_HEIGHT/size_2)-i+"", s_HEIGHT/2+5, i*size-2+15);
				g.drawString("-"+((s_HEIGHT/size_2)-i)+"", i*size+5, s_HEIGHT/2+15);
			}else{
				g.drawString("-"+(i-(s_HEIGHT/size_2))+"", s_HEIGHT/2+5, i*size-2+15);
				g.drawString((i-(s_HEIGHT/size_2))+"", i*size+5, s_HEIGHT/2+15);
			}
		}
	}
	public static void main(String[] args) {
		JFrame f= new JFrame();
		Graph g= new Graph();
		//f.add(g);
		f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		f.setBounds(200, 500, s_HEIGHT, s_HEIGHT+size);
		f.getContentPane().add(g);
		f.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		repaint();
		
	}
}

